package com.ihd.ihardware.view.tzc.health.model;

import android.databinding.ObservableArrayList;

import com.google.gson.JsonObject;
import com.ihd.ihardware.R;
import com.ihd.ihardware.api.HttpClient;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.pojo.DeviceListRes;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.pojo.NormRes;
import com.ihd.ihardware.pojo.ReportRes;
import com.ihd.ihardware.pojo.ReportsRes;
import com.ihd.ihardware.pojo.StepEntity;
import com.ihd.ihardware.pojo.Test_rp;
import com.ihd.ihardware.pojo.WeightsRes;

import org.json.JSONArray;
import org.json.JSONObject;
import org.reactivestreams.Publisher;

import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.commonlibrary.http.BaseSubscriber;
import cn.wowjoy.commonlibrary.utils.RxSchedulerTransformer;
import cn.wowjoy.commonlibrary.viewmodel.BaseRepository;
import io.reactivex.Flowable;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class ReportRepository extends BaseRepository {

    public void getRPList(ObservableArrayList<Test_rp> mDT, ReportRes.DataBean rd) {
        mDT.clear();
        mDT.add(new Test_rp(R.drawable.icon_body_2ttizhong, "体重", rd.getWeight() + "公斤", rd.getBmiFlag(), rd.getBmiDes(), 17));
        mDT.add(new Test_rp(R.drawable.icon_body_3bmi, "BMI", rd.getBmi() + "", rd.getBmiFlag(), rd.getBmiDes(), 2));
        if (rd.getObesity() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_1feipangdu, "肥胖度", String.format("%.2f", rd.getObesity() * 100) + "%", rd.getObesityFlag(), rd.getObesityDes(), 1));
        if (rd.getFat() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_4zhifang, "体脂率", String.format("%.2f", rd.getFat() * 100) + "%", rd.getFatFlag(), rd.getFatDes(), 3));
        if (rd.getFatWeight() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_5zhifangzhongliang, "脂肪重量", rd.getFatWeight() + "公斤", rd.getFatWeightFlag(), rd.getFatWeightDes(), 4));
        if (rd.getSkeletalMuscle() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_6gugejilv, "骨骼肌率", String.format("%.2f", rd.getSkeletalMuscle() * 100) + "%", rd.getSkeletalMuscleFlag(), rd.getSkeletalMuscleDes(), 5));
        if (rd.getSkeletalMuscleWeight() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_7gugezhongliang, "骨骼肌重量", rd.getSkeletalMuscleWeight() + "公斤", rd.getSkeletalMuscleWeightFlag(), rd.getSkeletalMuscleWeightDes(), 7));
        if (rd.getMuscleWeight() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_8jirouzhongliang, "肌肉重量", rd.getMuscleWeight() + "公斤", rd.getMuscleWeightFlag(), rd.getMuscleWeightDes(), 8));
        if (rd.getMuscle() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_9jiroulv, "肌肉率", String.format("%.2f", rd.getMuscle() * 100) + "%", rd.getMuscleFlag(), rd.getMuscleDes(), 6));
        if (rd.getVisceralFat() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_10neizang, "内脏脂肪", rd.getVisceralFat() + "级", rd.getVisceralFatFlag(), rd.getVisceralFatDes(), 9));
        if (rd.getMoisture() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_11shufien, "水份", String.format("%.2f", rd.getMoisture() * 100) + "%", rd.getMoistureFlag(), rd.getMoistureDes(), 10));
        if (rd.getMoistureWeight() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_12shuihanliang, "水含量", rd.getMoistureWeight() + "公斤", rd.getMoistureWeightFlag(), rd.getMoistureWeightDes(), 11));
        if (rd.getBasalMetabolism() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_13jichudaixie, "基础代谢", rd.getBasalMetabolism() + "", rd.getBasalMetabolismFlag(), rd.getBasalMetabolismDes(), 12));
        if (rd.getBoneMass() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_14guliang, "骨量", rd.getBoneMass() + "公斤", rd.getBoneMassFlag(), rd.getBoneMassDes(), 13));
        if (rd.getProtein() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_15danbaizhi, "蛋白质", String.format("%.2f", rd.getProtein() * 100) + "%", rd.getProteinFlag(), rd.getProteinDes(), 14));
        if (rd.getGreaseWeight() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_16quzhitizhong, "去脂体重", rd.getGreaseWeight() + "公斤", "", "", 15));
        if (rd.getBodyAge() != 0)
            mDT.add(new Test_rp(R.drawable.icon_body_17shentininaling, "身体年龄", rd.getBodyAge() + "", "", "", 16));
    }

    public void bind(String originalData) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("originalData", originalData);
        mParam.addProperty("type", "0");
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().bind(rb)
                .flatMap(new Function<EmptyRes, Publisher<DeviceListRes>>() {
                    @Override
                    public Publisher<DeviceListRes> apply(EmptyRes emptyRes) throws Exception {
                        if (emptyRes.getCode() == 0)
                            return HttpClient.getService().deviceList(1, 100);
                        else
                            return Flowable.empty();
                    }
                })
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<DeviceListRes>(AppConstans.BIND) {
                }));
    }

    public void weighing(String familyId, String originalData) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("familyId", familyId);
        mParam.addProperty("originalData", originalData);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().weighing(rb)
                .flatMap(new Function<EmptyRes, Publisher<ReportRes>>() {
                    @Override
                    public Publisher<ReportRes> apply(EmptyRes emptyRes) throws Exception {
                        return HttpClient.getService().report(familyId);
                    }
                })
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<ReportRes>(AppConstans.REPORT) {
                }));
    }

    public void handWeighing(String familyId, String weight) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("familyId", familyId);
        mParam.addProperty("weight", weight);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().handWeighing(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<EmptyRes>(AppConstans.HANDWEIGHT) {
                }));
    }

    public void getWeights(String familyId, String type) {
        addDisposable(HttpClient.getService().getWeights(familyId, type)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<WeightsRes>(AppConstans.WEIGHTS) {
                }));
    }

    public void reports(String familyId, String type) {
        addDisposable(HttpClient.getService().reports(familyId, type)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<ReportsRes>(AppConstans.REPORTS) {
                }));
    }

    public void norm(String id) {
        addDisposable(HttpClient.getService().norms(id)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<NormRes>(AppConstans.NORM) {
                }));
    }

    public void delWeighing(String id) {
        addDisposable(HttpClient.getService().delWeighing(id)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<EmptyRes>(AppConstans.DELWEIGHT) {
                }));
    }

    public void batchStepNumber(List<StepEntity> datas) {
        try {
            JSONArray jsonArray = new JSONArray();
            for (StepEntity s : datas) {
                JSONObject jo = new JSONObject();
                jo.put("stepNumber", Integer.parseInt(s.getSteps()));
                jo.put("time", s.getCurDate());
                jsonArray.put(jo);
            }
            JSONObject jo = new JSONObject();
            jo.put("stepNumbers", jsonArray);
            RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), jo.toString());
            addDisposable(HttpClient.getService().batchStepNumber(rb)
                    .compose(new RxSchedulerTransformer<>())
                    .subscribeWith(new BaseSubscriber<EmptyRes>(0) {
                    }));
        } catch (Exception e) {

        }
    }
}
